package com.workday.gridsimulator

import com.codahale.jerkson.Json._
import com.codahale.jerkson.ParsingException


object JsonHelper {

  def parseGridFromFile(filePath: String): GridDetails = {
    val file = new java.io.File(filePath)
    try {
      val details = parse[GridDetails](file)
      details
    } catch {
      case pe: ParsingException =>
        throw pe
    }
  }
  
  def generateGridJsonFile(simType: String, workerCount: String, jobCount: String, arrivalRate: String, avgLength: String, ec2InstanceSize: String, filePath: String) = {
    val gridDetails = GridDetails(simType, workerCount, jobCount, arrivalRate, avgLength, ec2InstanceSize)
    val json = generate(gridDetails)
    
    val file = new java.io.File(filePath)
    file.createNewFile()
    val writer = new java.io.PrintWriter(file)
    writer.write(json)
    writer.close
    
    file
  }

}

case class GridDetails(simType: String, workerCount: String, jobCount: String, arrivalRate: String, avgLength: String, ec2InstanceSize: String)